package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.limao.DBDataClass.AdvDataClass;
import com.limao.DBProvider.CrowdAdvProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class sys_AdvCacheManager  extends BasicModule{
	
	private static String SysWelcomeAdvKey="SysWelcomeAdvKey";
	private static String SysHomeAdvkey="SysHomeAdvkey";

	private static sys_AdvCacheManager instance;
	private CrowdAdvProvider provider;
	
    public static sys_AdvCacheManager getInstance() {
        return instance;
    }

    private Cache<String, String> WelcomeCacheManager;
    private Cache<String, String> SysHomeAdv_CacheManager;
    

    public sys_AdvCacheManager() {
    	super("sys_AdvCacheManager");
    	WelcomeCacheManager = CacheFactory.createCache("sys_cd_welcomeCache");
    	SysHomeAdv_CacheManager = CacheFactory.createCache("sys_cd_sysAdvCache");
    	initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    

    public void ClearSysWelcomeCache()
    {
    	WelcomeCacheManager.clear();
    }  
    
    public void ClearSysHomeAdvCache()
    {
    	SysHomeAdv_CacheManager.clear();
    } 

    
    //**************************************************************************

    //懒猪  应用的欢迎页广告图片
    public String Load_WeclomeAdv(int pos)
    {
    	String imgurl=WelcomeCacheManager.get(SysWelcomeAdvKey);
    	if(imgurl==null)
    	{
    		AdvDataClass obj=provider.Load_welcomeAdv();
    		if(obj!=null)
    		{
    			WelcomeCacheManager.put(SysWelcomeAdvKey,obj.imgurl); 
    		}
    			
    	}
    	return imgurl;
    } 
    
    
    
    //懒猪  系统素材
    public List<AdvDataClass> Load_SysHomeAdv()
    {
    	
    	String json=SysHomeAdv_CacheManager.get(SysHomeAdvkey);
		if (CacheClass.isEmpty(json)) 
		{
			ArrayList<AdvDataClass> list = provider.Load_SySMatter_All();
			json=JSON.toJSONString(list);
			if (list.size()>0) 
			{
				SysHomeAdv_CacheManager.put(SysHomeAdvkey, json);
			}
		}
		
		List<AdvDataClass> list=new ArrayList<AdvDataClass>();
		if (!CacheClass.isEmpty(json))
		{
			AdvDataClass[] nodes = JSON.parseObject(json, new TypeReference<AdvDataClass[]>(){});    
			list.addAll(Arrays.asList(nodes));
		}
    	
    	return list;
    }  
    
    
    
    //**************************************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =CrowdAdvProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
}
